package com.ruoyi.store.service.buy;

import com.ruoyi.caigou.api.RemoteBuyOrderService;
import com.ruoyi.caigou.domain.BuyOrder;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.finance.api.RemoteBuyMustPayService;
import com.ruoyi.finance.domain.BuyMustPay;
import com.ruoyi.store.mapper.StockInDetailMapper;
import com.ruoyi.store.mapper.StockInMapper;
import com.ruoyi.system.api.domain.store.StockIn;
import com.ruoyi.system.api.domain.store.StockInDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class BuyMustPayService {
    @Autowired
    private StockInMapper stockInMapper;
    @Autowired
    private StockInDetailMapper stockInDetailMapper;
    @Autowired
    private RemoteBuyMustPayService remoteBuyMustPayService;

    @Autowired
    private RemoteBuyOrderService remoteBuyOrderService;

    public Boolean addBuyMustPay(BuyMustPay buyMustPay) {
        R<Boolean> r = remoteBuyMustPayService.addBuyMustPay(buyMustPay, SecurityConstants.INNER);
        return r.getData();
    }

    @Transactional
    public Boolean addBuyMustPayByStockIn(StockIn stockIn) {
        if (stockIn.getInventoryType() != 1) {
            return false;
        }
        String orderCode = stockIn.getOrderCode();
        R<BuyOrder> remoteBuyOrder = remoteBuyOrderService.getInfoByCode(orderCode, SecurityConstants.INNER);
        if (remoteBuyOrder.getCode() == R.FAIL) {
            return false;
        }
        BuyOrder buyOrder = remoteBuyOrder.getData();  //采购订单

        StockInDetail stockInDetail = new StockInDetail();
        stockInDetail.setStockInId(stockIn.getId());
        List<StockInDetail> stockInDetails = stockInDetailMapper.selectStockInDetailList(stockInDetail);


        BuyMustPay buyMustPay = new BuyMustPay();
        buyMustPay.setCode("CGYF-" + System.currentTimeMillis());
        buyMustPay.setSourceCode(stockIn.getStockInCode());
        buyMustPay.setDocTime(new Date());
        buyMustPay.setSupId(buyOrder.getSupId());
        buyMustPay.setTotalAmount(stockIn.getCost());
        buyMustPay.setVerifiedAmount(BigDecimal.ZERO);
        buyMustPay.setBusinessUserId(buyOrder.getBusinessUserId());
//        buyMustPay.setVerifiedAmount(new BigDecimal(0));
//        buyMustPay.set(buyOrder.getBusinessUserId());

        //更新订单
        //采购订单编号
        Integer totalNum = stockIn.getTotalNum();//入库总数量
        buyOrder.setInventoryQuantity(buyOrder.getInventoryQuantity() + totalNum);//已入库数量
        buyOrder.setInventoryAmount(
                stockIn.getCost().add(buyOrder.getInventoryAmount() == null ? BigDecimal.ZERO : buyOrder.getInventoryAmount())
        );//已入库金额

        //入库金额等于订单总金额  全部入库
        buyOrder.setStoreStatus(
                buyOrder.getInventoryAmount().compareTo(buyOrder.getTotalAmount()) == 0 ? 2 : 1
        );


        R<Boolean> booleanR = remoteBuyOrderService.innerEdit(buyOrder, SecurityConstants.INNER);
        if (R.FAIL == booleanR.getCode()) {
            throw new ServiceException("订单更新失败");
        }
        return addBuyMustPay(buyMustPay);
    }
}
